// pages/xq/xq.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
const {
  ly_xq,
  getGwc,
  getfav,
  getcheck,
  getGwcs,
  delfav
} = require('../../http/api')
var WxParse = require('../../wxParse/wxParse.js');
const { add } = require('../../util/getCart/getCart')
Page({
  onChange(event) {
    this.setData({
      num: event.detail
    })
  },
  /**
   * 页面的初始数据
   */

  data: {
    bindtaps: "",
    indexC: '',
    color: '',
    ID: '',
    num: 1,
    fff: 'none',
    currentIndex: 0,
    data: {},
    top: [{
      name: "商品简介"
    },
    {
      name: "商品详情"
    },
    {
      name: "商品评价"
    },
    ],
    swipe: [{
      url: "https://hundred-battles.gitee.io/cloud-cumulus-system/swipe_4.png"
    },
    {
      url: "https://hundred-battles.gitee.io/cloud-cumulus-system/swipe_3.png"
    }
    ],
    indicatorDots: true,
    vertical: true,
    autoplay: true,
    interval: 2000,
    duration: 500,
    circular: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  color(e) {
    this.setData({
      indexC: e.currentTarget.dataset.index,
      color: JSON.stringify([{ optionId: e.currentTarget.dataset.pid, optionValueId: e.currentTarget.dataset.id }]),
    })

  },
  // 添加删除收藏
  async fav() {
    let res_s = await this.addDel()
    if (res_s == '已收藏') {
      delfav(this.data.ID, wx.getStorageSync('token')).then(res => {
        // console.log(res);
        this.addDel().then(res => {
          Toast('删除收藏');
          this.setData({
            bindtaps: res
          })
        })
      })
    } else {
      getfav(this.data.ID, wx.getStorageSync('token')).then(res => {
        // console.log(res);
        this.addDel().then(res => {
          Toast('添加收藏');
          this.setData({
            bindtaps: res
          })
        })
      })
    }

  },
  // 解决回调地狱
  addDel() {
    return new Promise((resolve, reject) => {
      getcheck(this.data.ID, wx.getStorageSync('token')).then(res => {
        resolve(res.data.data)
      })
    })
  },
  onLoad: function (options) {
    // console.log(options);
    this.setData({
      ID: options.id
    })
    ly_xq(options.id, wx.getStorageSync('token')).then(res => {
      // console.log(res.data.data);
      this.setData({
        data: res.data.data,
        ID: options.id
      })
      var that = this;
      WxParse.wxParse('article', 'html', this.data.data.content, that, 0);
    })
  },
  cart: function () {
    wx.switchTab({
      url: '/pages/gwc/gwc',
    })
  },
  adds() {
    this.setData({
      fff: 'none'
    })

    if (this.data.data.properties) {
      console.log(this.data.color);
      getGwc(this.data.ID, this.data.num, this.data.color, wx.getStorageSync('token')).then(res => {
        console.log(res);
        wx.showToast({
          title: '加入成功',
          icon: 'success',
          duration: 2000
        })
        
      })
    } else {
      getGwcs(wx.getStorageSync('token'), this.data.ID, this.data.num).then(res => {
        wx.showToast({
          title: '加入成功',
          icon: 'success',
          duration: 2000
        })

      })
    }

  },
  show_gm: function () {
    this.setData({
      fff: 'block'
    })
  },
  del: function () {
    this.setData({
      fff: 'none'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(this.data.ID);
    getcheck(this.data.ID, wx.getStorageSync('token')).then(res => [
      // console.log(res)
      this.setData({
        bindtaps: res.data.data
      })
    ])
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})